//
//  RegisterDeviceIDApi.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/28.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "InfraredApi.h"
#import "NSString+Password.h"
#import "NSString+FIDExtra.h"
//#import "AFURLRequestSerialization.h"

@implementation InfraredApi
{
    NSDictionary *_dic;
}

-(id)initWithDic:(NSDictionary *)dic{
    if (self = [super init]) {
        _dic = dic;
    }
    return self;
}

- (NSString *)requestUrl {
    return @"";
}

- (YTKRequestMethod)requestMethod {
    return YTKRequestMethodPOST;
}
- (NSString *)baseUrl
{
    return @"http://api.yaokongyun.cn/chip/m.php";
}

- (ServerType)serverType
{
    return Infrared;
}

- (NSDictionary<NSString *,NSString *> *)requestHeaderFieldValueDictionary
{
    __block  NSString *string = [NSString string];
    NSString *sign = [NSString string];
    NSString *client = [NSString string];
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:_dic];
    [dict removeObjectForKey:@"c"];
    [dict removeObjectForKey:@"appid"];
    
//    NSArray *valueArray = dict.allValues;
    NSArray *keyArray = dict.allKeys;
    NSArray *keySortedArray = [keyArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [obj1 compare:obj2 options:NSNumericSearch];
    }];

    for (NSString *str in keySortedArray)
    {
        string = [string stringByAppendingString:dict[str]];
    }
//    [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
//        NSLog(@"key = %@ and obj = %@", key, obj);
//       string = [string stringByAppendingString:obj];
//    }];
    NSLog(@"参数%@",string);
    string = [string stringByAppendingString:[self getNowTimeTimestamp]];
    string = [string MD5];
    NSArray *array = @[@1,@3,@7,@15,@31];
    if (string.length == 32)
    {
        for (NSNumber *num in array)
        {
            sign = [sign stringByAppendingString:string.subString([num intValue],[num intValue]+1)];
        }
    }
    client = [NSString stringWithFormat:@"%@_%@",[self getNowTimeTimestamp],sign];
    return @{@"client":client};
}

//NSString * AFQueryStringFromParameters(NSDictionary *parameters) {
//    NSMutableArray *mutablePairs = [NSMutableArray array];
//    for (AFQueryStringPair *pair in AFQueryStringPairsFromDictionary(parameters)) {
//        [mutablePairs addObject:[pair URLEncodedStringValue]];
//    }
//
//    return [mutablePairs componentsJoinedByString:@"&"];
//}


//- (NSDictionary *)responseHeaders
//{
//    __block  NSString *string = [NSString string];
//    NSString *sign = [NSString string];
//    NSString *client = [NSString string];
//    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:_dic];
//    [dict removeObjectForKey:@"c"];
//    [dict removeObjectForKey:@"appid"];
//
//    [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
//        NSLog(@"key = %@ and obj = %@", key, obj);
//        string = [string stringByAppendingString:obj];
//    }];
//    NSLog(@"参数%@",string);
//    string = [string stringByAppendingString:[self getNowTimeTimestamp]];
//    string = [string MD5];
//    NSArray *array = @[@1,@3,@7,@15,@31];
//    if (string.length == 32)
//    {
//        for (NSNumber *num in array)
//        {
//            sign = [sign stringByAppendingString:string.subString([num intValue],[num intValue]+1)];
//        }
//    }
//    client = [NSString stringWithFormat:@"%@_%@",[self getNowTimeTimestamp],sign];
//
//    return @{@"client":client,@"Content-Type":@"application/x-www-form-urlencoded"};
//}

- (NSString *)getNowTimeTimestamp
{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970];
    NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
    return timeString;
}

- (id)requestArgument {
//    NSMutableDictionary *dic = [NSMutableDictionary getPubdic];
//
//    [dic setValuesForKeysWithDictionary:_dic];
//    NSString *sign=[NSString getSign:dic];
//    [dic setObject:sign forKey:@"sign"];
    return _dic;
}


-(void)dealloc
{
    NSLog(@"%@ dealloc",[self class]);
}
@end
